﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;

namespace Emilie.Animation
{
    public static class ThemeAnimations
    {
        public static Storyboard Entrance(IEnumerable<UIElement> targets, Double staggerOffset, TimeSpan staggerDuration, TimeSpan slideDuration)
        {
            var sb = new Storyboard();

            var start = TimeSpan.FromSeconds(0);

            foreach (FrameworkElement t in targets)
            {
                t.EnsureCompositeTransform();

                // Translation Keyspline
                // Duration 1000ms
                DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
                da.AddEasingDoubleKeyFrame(TimeSpan.FromSeconds(0), staggerOffset);
                da.AddEasingDoubleKeyFrame(start, staggerOffset);
                da.AddSplineDoubleKeyFrame(start.Add(slideDuration), 0, KeySplines.EntranceTheme);
                sb.AddTimeline(da, t, TargetProperty.RenderTransformTranslateX);


                // Opacity keyspline
                // Duration 170ms
                var daO = new DoubleAnimationUsingKeyFrames();
                daO.AddEasingDoubleKeyFrame(TimeSpan.FromSeconds(0), 0);
                daO.AddEasingDoubleKeyFrame(start, 0);
                daO.AddSplineDoubleKeyFrame(start.Add(TimeSpan.FromMilliseconds(170)), 1, KeySplines.EntranceTheme);
                sb.AddTimeline(daO, t, TargetProperty.Opacity);


                // Offset the start of the next animation
                start = start.Add(staggerDuration);
            }

            return sb;
        }

        public static Storyboard PopUp(FrameworkElement target, Double YOffset)
        {
            var sb = new Storyboard();

            var start = TimeSpan.FromSeconds(0);

            //foreach (FrameworkElement t in targets)
            {
                target.EnsureCompositeTransform();

                // Translation Keyspline
                // Duration 1000ms
                DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
                da.AddEasingDoubleKeyFrame(TimeSpan.FromSeconds(0), YOffset);
                da.AddSplineDoubleKeyFrame(TimeSpan.FromMilliseconds(367), 0, KeySplines.Popup);
                sb.AddTimeline(da, target, TargetProperty.RenderTransformTranslateY);


                // Opacity keyspline
                // Duration 170ms
                var daO = new DoubleAnimationUsingKeyFrames();
                daO.AddEasingDoubleKeyFrame(TimeSpan.FromSeconds(0), 0);
                daO.AddSplineDoubleKeyFrame(TimeSpan.FromMilliseconds(83), 1, KeySplines.EntranceTheme);
                sb.AddTimeline(daO, target, TargetProperty.Opacity);


                // Offset the start of the next animation
                //start = start.Add(staggerDuration);
            }

            return sb;
        }
    }
}
